package localizer.main;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BdAdabter {
	public static final String KEY_USERNAME = "username";
    public static final String KEY_ID = "_id";
    private static final String DATABASE_NAME = "data"; 
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_TABLE = "user"; 
    private static final String DATABASE_TABLE2 = "timeLoc";
    private static final String DATABASE_TABLE3 = "timeTask";
    private static final String DATABASE_TABLE4 = "timeRing";
    private static final String DATABASE_TABLE5 = "password";
    private static final String DATABASE_TABLE6 = "phone_number";
    private static final String DATABASE_TABLE7 = "check_update_gps";
    private static final String DATABASE_TABLE8 = "check_update_tasks";
    private static final String DATABASE_TABLE9 = "check_ring";
    private static final String DATABASE_TABLE10 = "check_ring_by_msg";
    private static final String DATABASE_TABLE11 = "check_loc_by_msg";
    private static final String DATABASE_TABLE12 = "lastloc";
    private static final String DATABASE_TABLE13 = "simid";
    private static final String DATABASE_TABLE14 = "emergency_phone";
    private static final String DATABASE_TABLE15 = "check_check_chip";
    private static final String DATABASE_CREATE =
            "CREATE TABLE user (_id TEXT PRIMARY KEY , "
            + "username TEXT NOT NULL);" ;
    private static final String DATABASE_CREATE2 =
        "create table timeLoc (_id integer primary key autoincrement, "
        + "time TEXT NOT NULL );" ;
    private static final String DATABASE_CREATE3 =
        "create table timeTask (_id integer primary key autoincrement, "
        + "time TEXT NOT NULL );" ;
    private static final String DATABASE_CREATE4 =
        "create table timeRing (_id integer primary key autoincrement, "
        + "time TEXT NOT NULL );" ;
    private static final String DATABASE_CREATE5 =
        "create table password (_id integer primary key autoincrement, "
        + "password TEXT NOT NULL );" ;
    private static final String DATABASE_CREATE6 =
            "create table "+DATABASE_TABLE6+ " (_id integer primary key autoincrement, "
            + "phone_number TEXT NOT NULL );" ;
    private static final String DATABASE_CREATE7 =
            "create table "+DATABASE_TABLE7+ " (_id integer primary key autoincrement, "
            + "check_update_gps integer NOT NULL );" ;
    private static final String DATABASE_CREATE8 =
            "create table "+DATABASE_TABLE8+ " (_id integer primary key autoincrement, "
            + "check_update_tasks integer NOT NULL );" ;
    private static final String DATABASE_CREATE9 =
            "create table "+DATABASE_TABLE9+ " (_id integer primary key autoincrement, "
            + "check_ring integer NOT NULL );" ;
    private static final String DATABASE_CREATE10 =
            "create table "+DATABASE_TABLE10+ " (_id integer primary key autoincrement, "
            + "check_ring_by_msg integer NOT NULL );" ;
    private static final String DATABASE_CREATE11 =
            "create table "+DATABASE_TABLE11+ " (_id integer primary key autoincrement, "
            + "check_loc_by_msg integer NOT NULL );" ;
    private static final String DATABASE_CREATE12 =
    	 "create table "+DATABASE_TABLE12+ " (_id integer primary key autoincrement, "
         + "lat TEXT NOT NULL, long TEXT NOT NULL, phone TEXT NOT NULL, data TEXT NOT NULL );" ;
    private static final String DATABASE_CREATE13 =
       	 "create table "+DATABASE_TABLE13+ " (_id integer primary key autoincrement,simid TEXT NOT NULL)" ;
    private static final String DATABASE_CREATE14 =
          	 "create table "+DATABASE_TABLE14+ " (_id integer primary key autoincrement, " + DATABASE_TABLE14+" TEXT NOT NULL)" ;
    private static final String DATABASE_CREATE15 =
            "create table "+DATABASE_TABLE15+ " (_id integer primary key autoincrement, "
            + DATABASE_TABLE15 +" integer NOT NULL );" ;
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    private Context context;
    
    public BdAdabter(Context context) {

    	this.context=context;
    }
    public void open() throws SQLException {
        mDbHelper = new DatabaseHelper(context);
        mDb = mDbHelper.getWritableDatabase();
    }

    public void close() {
        mDbHelper.close();
    }
    public Cursor fetchAllUsers() {

    	return mDb.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_USERNAME}, null, null, null, null, null);
    }
    
    public long createUser(String username, String id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ID, id);
        initialValues.put(KEY_USERNAME, username);
        

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }
    
    public boolean hasUser() {
    	boolean resp=false;
    	Cursor cur = fetchAllUsers();
    	cur.moveToFirst();
    	if(cur.getCount() >0){
    		resp=true;
    	}
    	return resp;
    }
    
    public Cursor fetchAllTimeLoc() {

    	return mDb.query(DATABASE_TABLE2, new String[] {"time"}, null, null, null, null, null);
    }
    public Cursor fetchAllPhoneNumber() {

    	return mDb.query(DATABASE_TABLE6, new String[] {"phone_number"}, null, null, null, null, null);
    }
    
    public long createPhoneNumber(String phoneNumber) {
        ContentValues initialValues = new ContentValues();
    initialValues.put("phone_number", phoneNumber);
        

        return mDb.insert(DATABASE_TABLE6, null, initialValues);
    }
    
    public long createTimeLoc(String username) {
        ContentValues initialValues = new ContentValues();
    initialValues.put("time", username);
        

        return mDb.insert(DATABASE_TABLE2, null, initialValues);
    }
    
    public boolean hasTimeLoc() {
    	boolean resp=false;
    	Cursor cur = fetchAllTimeLoc();
    	cur.moveToFirst();
    	if(cur.getCount() >0){
    		resp=true;
    	}
    	return resp;
    }
    
    public String getTimeLoc() {
    	Cursor cur = fetchAllTimeLoc();
    	cur.moveToFirst();
    	return cur.getString(0);
    }
    public String getPhoneNumber() {
    	Cursor cur = fetchAllPhoneNumber();
    	cur.moveToFirst();
    	return cur.getString(0);
    }
    
    public void dropTableLoc(){
    	mDb.delete(DATABASE_TABLE2,null,null );
    }
    public void dropTablePhoneNumber(){
    	mDb.delete(DATABASE_TABLE6,null,null );
    }
    
    public Cursor fetchAllTimeTask() {

    	return mDb.query(DATABASE_TABLE3, new String[] {"time"}, null, null, null, null, null);
    }
    
    public long createTimeTask(String username) {
        ContentValues initialValues = new ContentValues();
    initialValues.put("time", username);
        

        return mDb.insert(DATABASE_TABLE3, null, initialValues);
    }
    
    public boolean hasTimeTask() {
    	boolean resp=false;
    	Cursor cur = fetchAllTimeTask();
    	cur.moveToFirst();
    	if(cur.getCount() >0){
    		resp=true;
    	}
    	return resp;
    }
    
    public String getTimeTask() {
    	Cursor cur = fetchAllTimeTask();
    	cur.moveToFirst();
    	return cur.getString(0);
    }
    
    public void dropTableTask(){
    	mDb.delete(DATABASE_TABLE3,null,null );
    }
    
    public Cursor fetchAllTimeRing() {

    	return mDb.query(DATABASE_TABLE4, new String[] {"time"}, null, null, null, null, null);
    }
    
    public long createTimeRing(String username) {
        ContentValues initialValues = new ContentValues();
    initialValues.put("time", username);
        

        return mDb.insert(DATABASE_TABLE4, null, initialValues);
    }
    
    
    public boolean hasTimeRing() {
    	boolean resp=false;
    	Cursor cur = fetchAllTimeRing();
    	cur.moveToFirst();
    	if(cur.getCount() >0){
    		resp=true;
    	}
    	return resp;
    }
    
    public String getTimeRing() {
    	Cursor cur = fetchAllTimeRing();
    	cur.moveToFirst();
    	return cur.getString(0);
    }
    
    public void dropTableRing(){
    	mDb.delete(DATABASE_TABLE4,null,null );
    }
    
    public Cursor fetchAllPassword() {

    	return mDb.query(DATABASE_TABLE5, new String[] {"password"}, null, null, null, null, null);
    }
    
    public long createPassword(String password) {
        ContentValues initialValues = new ContentValues();
    initialValues.put("password", password);
        

        return mDb.insert(DATABASE_TABLE5, null, initialValues);
    }
    
    
    public boolean hasPassword() {
    	boolean resp=false;
    	Cursor cur = fetchAllPassword();
    	cur.moveToFirst();
    	if(cur.getCount() >0){
    		resp=true;
    	}
    	return resp;
    }
    
    public String getPassword() {
    	Cursor cur = fetchAllPassword();
    	cur.moveToFirst();
    	return cur.getString(0);
    }
    
    public void dropTablePassword(){
    	mDb.delete(DATABASE_TABLE5,null,null );
    }
    
    public void dropTable(){
    	mDb.delete(DATABASE_TABLE,null,null );
    }
    public String getUsername() {
    	Cursor cur = fetchAllUsers();
    	cur.moveToFirst();
    	return cur.getString(1);
    }

    public String getId() {
    	Cursor cur = fetchAllUsers();
    	cur.moveToFirst();
    	return cur.getString(0);
    }
    
    public String getLat() {
    	Cursor cur = fetchAllLoc();
    	cur.moveToFirst();
    	return cur.getString(0);
    }

    public String getLong() {
    	Cursor cur = fetchAllLoc();
    	cur.moveToFirst();
    	return cur.getString(1);
    }
    
    public String getPhone() {
    	Cursor cur = fetchAllLoc();
    	cur.moveToFirst();
    	return cur.getString(2);
    }
    
    public String getData() {
    	Cursor cur = fetchAllLoc();
    	cur.moveToFirst();
    	return cur.getString(3);
    }
    
    public Cursor fetchAllLoc() {

    	return mDb.query(DATABASE_TABLE12, new String[] {"lat", "long","phone" ,"data"}, null, null, null, null, null);
    }
    
    public long createLoc(String lat, String longi,String phone, String data) {
        ContentValues initialValues = new ContentValues();
        initialValues.put("lat", lat);
        initialValues.put("long", longi);
        initialValues.put("phone", phone);
        initialValues.put("data", data);

        return mDb.insert(DATABASE_TABLE12, null, initialValues);
    }
    
    public void dropTableLocalization(){
    	mDb.delete(DATABASE_TABLE12,null,null );
    }
    
    public boolean hasLoc() {
    	boolean resp=false;
    	Cursor cur = fetchAllLoc();
    	cur.moveToFirst();
    	if(cur.getCount() >0){
    		resp=true;
    	}
    	return resp;
    }
    
   
	private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE2);
            db.execSQL(DATABASE_CREATE3);
            db.execSQL(DATABASE_CREATE4);
            db.execSQL(DATABASE_CREATE5);
            db.execSQL(DATABASE_CREATE6);
            db.execSQL(DATABASE_CREATE7);
            db.execSQL(DATABASE_CREATE8);
            db.execSQL(DATABASE_CREATE9);
            db.execSQL(DATABASE_CREATE10);
            db.execSQL(DATABASE_CREATE11);
            db.execSQL(DATABASE_CREATE12);
            db.execSQL(DATABASE_CREATE13);
            db.execSQL(DATABASE_CREATE14);
            db.execSQL(DATABASE_CREATE15);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            
            db.execSQL("DROP TABLE IF EXISTS user");
            db.execSQL("DROP TABLE IF EXISTS timeLoc");
            db.execSQL("DROP TABLE IF EXISTS timeRing");
            db.execSQL("DROP TABLE IF EXISTS timeTask");
            db.execSQL("DROP TABLE IF EXISTS password");
            db.execSQL("DROP TABLE IF EXISTS phone_number");
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE7);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE8);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE9);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE10);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE11);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE12);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE13);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE14);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE15);
            onCreate(db);
        }
    }
	
	
	public int getEnableCheckUpdateLocation() {
    	Cursor cur = fetchAllEnableCheckUpdateLocation();
    	cur.moveToFirst();
    	int ret =cur.getInt(0);
    	return ret;
    }
    private Cursor fetchAllEnableCheckUpdateLocation() {
    	return mDb.query(DATABASE_TABLE7, new String[] {DATABASE_TABLE7}, null, null, null, null, null);
	}
    public void dropTableEnableCheckUpdateLocation(){
    	mDb.delete(DATABASE_TABLE7,null,null );
    }
    public long createEnableCheckUpdateLocation(int i) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(DATABASE_TABLE7, i);
        return mDb.insert(DATABASE_TABLE7, null, initialValues);
    }
    
    
    
    
    public int getEnableUpdateTask() {
    	Cursor cur = fetchAllEnableUpdateTask();
    	cur.moveToFirst();
    	int ret =cur.getInt(0);
    	return ret;
    }
    private Cursor fetchAllEnableUpdateTask() {
    	return mDb.query(DATABASE_TABLE8, new String[] {DATABASE_TABLE8}, null, null, null, null, null);
	}
    public void dropEnableUpdateTask(){
    	mDb.delete(DATABASE_TABLE8,null,null );
    }
    public long createEnableUpdateTask(int i) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(DATABASE_TABLE8, i);
        return mDb.insert(DATABASE_TABLE8, null, initialValues);
    }
    
    
    
    public int getEnableCheckRing() {
    	Cursor cur = fetchAllEnableCheckRing();
    	cur.moveToFirst();
    	int ret =cur.getInt(0);
    	return ret;
    }
    private Cursor fetchAllEnableCheckRing() {
    	return mDb.query(DATABASE_TABLE9, new String[] {DATABASE_TABLE9}, null, null, null, null, null);
	}
    public void dropTableEnableCheckRing(){
    	mDb.delete(DATABASE_TABLE9,null,null );
    }
    public long createEnableCheckRing(int i) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(DATABASE_TABLE9, i);
        return mDb.insert(DATABASE_TABLE9, null, initialValues);
    }
    
    
    public int getEnableCheckRingByMsg() {
    	Cursor cur = fetchAllEnableCheckRingByMsg();
    	cur.moveToFirst();
    	int ret =cur.getInt(0);
    	return ret;
    }
    private Cursor fetchAllEnableCheckRingByMsg() {
    	return mDb.query(DATABASE_TABLE10, new String[] {DATABASE_TABLE10}, null, null, null, null, null);
	}
    public void dropTableEnableCheckRingByMsg(){
    	mDb.delete(DATABASE_TABLE10,null,null );
    }
    public long createEnableCheckRingByMsg(int i) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(DATABASE_TABLE10, i);
        return mDb.insert(DATABASE_TABLE10, null, initialValues);
    }
    
    
    
    public int getEnableCheckLocByMsg() {
    	Cursor cur = fetchAllEnableCheckLocByMsg();
    	cur.moveToFirst();
    	int ret =cur.getInt(0);
    	return ret;
    }
    private Cursor fetchAllEnableCheckLocByMsg() {
    	return mDb.query(DATABASE_TABLE11, new String[] {DATABASE_TABLE11}, null, null, null, null, null);
	}
    public void dropTableEnableCheckLocByMsg(){
    	mDb.delete(DATABASE_TABLE11,null,null );
    }
    public long createEnableCheckLocByMsg(int i) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(DATABASE_TABLE11, i);
        return mDb.insert(DATABASE_TABLE11, null, initialValues);
    }
    
    public ArrayList<String> getAllSimId(){
    	ArrayList<String> array = new ArrayList<String>();
    	Cursor cur = fetchAllSimId();
    	cur.moveToFirst();
    	while(cur.getCount()>0){
    		array.add(cur.getString(0));
    		if(cur.isLast()){
    			break;
    		}
    		cur.moveToNext();
    	}
    	
    	return array;
    }
    private Cursor fetchAllSimId() {
    	return mDb.query(DATABASE_TABLE13, new String[] {DATABASE_TABLE13}, null, null, null, null, null);
	}
    public void dropTableSimId(){
    	mDb.delete(DATABASE_TABLE13,null,null );
    }
    public long addSimid(String id) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(DATABASE_TABLE13, id);
        return mDb.insert(DATABASE_TABLE13, null, initialValues);
    }
    
    public String getEmergencyPhone(){
    	String resp="";
    	Cursor cur = fetchAllEmergencyPhone();
    	cur.moveToFirst();
    	if(cur.getCount()>0){
    		resp = cur.getString(0);
    	}
    	return  resp;
    }
	private Cursor fetchAllEmergencyPhone() {
		// TODO Auto-generated method stub
		return mDb.query(DATABASE_TABLE14, new String[] {DATABASE_TABLE14}, null, null, null, null, null);
	}
	public void dropTableEmergencyPhone(){
		mDb.delete(DATABASE_TABLE14,null,null );
	}
	public long addEmergencyPhone(String phone) {
		try {
			dropTableEmergencyPhone();
		} catch (Exception e) {
		}
        ContentValues initialValues = new ContentValues();
        initialValues.put(DATABASE_TABLE14, phone);
        return mDb.insert(DATABASE_TABLE14, null, initialValues);
    }
	
	public int getEnableCheckCheckSim() {
    	Cursor cur = fetchAllEnableCheckCheckSim();
    	cur.moveToFirst();
    	int ret =cur.getInt(0);
    	return ret;
    }
    private Cursor fetchAllEnableCheckCheckSim() {
    	return mDb.query(DATABASE_TABLE15, new String[] {DATABASE_TABLE15}, null, null, null, null, null);
	}
    public void dropTableEnableCheckCheckSim(){
    	mDb.delete(DATABASE_TABLE15,null,null );
    }
    public long createEnableCheckCheckSim(int i) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(DATABASE_TABLE15, i);
        return mDb.insert(DATABASE_TABLE15, null, initialValues);
    }
}

